//
//  YSMenuBottomView.m
//  YSBookRead
//
//  Created by MOLBASE on 2018/11/14.
//  Copyright © 2018年 YangShen. All rights reserved.
//

#import "YSMenuBottomView.h"
#import "Masonry.h"
#import "UIButton+YSLayout.h"

@interface YSMenuBottomView ()
@property (nonatomic, strong) UIButton *nextButton;
@property (nonatomic, strong) UIButton *prevButton;

@end

@implementation YSMenuBottomView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupSubViews];
    }
    return self;
}

- (void)setupSubViews {

    UIView *buttonBackView = [[UIView alloc] init];
    buttonBackView.backgroundColor = [UIColor colorWithRed:30/255 green:30/255 blue:30/255 alpha:1];
    [self addSubview:buttonBackView];
    [buttonBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(54);
    }];

    NSArray *titles = @[@"目录", @"设置", @"下载"];
    NSArray *images = @[@"directory", @"reading_more_setting", @"preview_btn"];
    CGFloat buttonW = [UIScreen mainScreen].bounds.size.width / titles.count;
    for (NSInteger i = 0; i < titles.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(i * buttonW, 0, buttonW, 54);
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        [button setImage:[[UIImage imageNamed:images[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] title:titles[i] titlePosition:YSTitlePositionBottom spacing:8];
        button.tag = i;
        [button addTarget:self action:@selector(bottomButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [buttonBackView addSubview:button];
    }

    UIButton *prevButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.prevButton = prevButton;
    [prevButton setTitle:@"上一章" forState:UIControlStateNormal];
    prevButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [prevButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [prevButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
    [prevButton addTarget:self action:@selector(prevButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:prevButton];

    [prevButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(buttonBackView.mas_top);
        make.width.mas_equalTo(60);
    }];

    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.nextButton = nextButton;
    [nextButton setTitle:@"下一章" forState:UIControlStateNormal];
    nextButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [nextButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
    [nextButton addTarget:self action:@selector(nextButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:nextButton];

    [nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(buttonBackView.mas_top);
        make.width.mas_equalTo(60);
    }];

    UISlider *slider = [[UISlider alloc] init];
    self.slider = slider;
    slider.minimumValue = 0;
    slider.maximumValue = 100;
    slider.value = 20;
    slider.minimumTrackTintColor = [UIColor orangeColor];
    slider.maximumTrackTintColor = [UIColor whiteColor];
    slider.thumbTintColor = [UIColor orangeColor];
    [slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:slider];

    [slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(prevButton.mas_right);
        make.right.mas_equalTo(nextButton.mas_left);
        make.centerY.mas_equalTo(prevButton.mas_centerY);
    }];
}

- (void)bottomButtonDidClick:(UIButton *)button {
    if (!self.delegate) {
        return;
    }

    if (button.tag == 0) {
        if ([self.delegate respondsToSelector:@selector(menuBottomView:didClickDirectoryButton:)]) {
            [self.delegate menuBottomView:self didClickDirectoryButton:button];
        }
    } else if (button.tag == 1) {
        if ([self.delegate respondsToSelector:@selector(menuBottomView:didClickSettingButton:)]) {
            [self.delegate menuBottomView:self didClickSettingButton:button];
        }
    } else {
        if ([self.delegate respondsToSelector:@selector(menuBottomView:didClickDownloadButton:)]) {
            [self.delegate menuBottomView:self didClickDownloadButton:button];
        }
    }
}

- (void)prevButtonDidClick {
    NSLog(@"上一章");
    NSInteger index = self.slider.value - 1 < 0 ? 0 : self.slider.value - 1;
    self.currentValue = index;
    if (self.delegate && [self.delegate respondsToSelector:@selector(menuBottomView:didChangeChapter:)]) {
        [self.delegate menuBottomView:self didChangeChapter:index];
    }
}

- (void)nextButtonDidClick {
    NSLog(@"下一章");
    NSInteger index = self.slider.value + 1 >= self.slider.maximumValue ? self.slider.maximumValue-1 : self.slider.value + 1;
    self.currentValue = index;
    if (self.delegate && [self.delegate respondsToSelector:@selector(menuBottomView:didChangeChapter:)]) {
        [self.delegate menuBottomView:self didChangeChapter:index];
    }
}

- (void)sliderValueChange:(UISlider *)slider {
    NSLog(@"slider: %f", slider.value);
    NSInteger index = (NSInteger)self.slider.value;
    self.currentValue = index;
    if (self.delegate && [self.delegate respondsToSelector:@selector(menuBottomView:didChangeChapter:)]) {
        [self.delegate menuBottomView:self didChangeChapter:index];
    }
}

- (void)setCurrentValue:(NSInteger)currentValue {
    _currentValue = currentValue;

    self.slider.value = currentValue;
    self.prevButton.enabled = currentValue != self.slider.minimumValue;
    self.nextButton.enabled = currentValue != self.slider.maximumValue;
}

@end
